﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileWebsiteGenerator.Entities;
using MobileWebsiteGenerator;

namespace MobileWebsiteGenerator.Areas.Admin.Controllers
{
    public class AdminPagesController : Controller
    {
        private Mobile_appContext db = new Mobile_appContext();

        //
        // GET: /Admin/AdminPages/

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Page/Create

        [HttpPost]
        public ActionResult Create(Page page)
        {
            if (ModelState.IsValid)
            {
                page.page_id = MvcApplication._MainCustomer.ContentPages != null && MvcApplication._MainCustomer.ContentPages.Count > 0 ? MvcApplication._MainCustomer.ContentPages.Count() + 1 : 1;
                MvcApplication._MainCustomer.ContentPages.Add(page);







                return RedirectToAction("EditWebsite", "AdminCustomer");
            }

            return View(page);
        }

        //
        // GET: /Page/Edit/5

        public ActionResult Edit(int id)
        {
            Page page = MvcApplication._MainCustomer.ContentPages.Single(x => x.page_id == id);
            return View(page);
        }

        //
        // POST: /Page/Edit/5

        [HttpPost]
        public ActionResult Edit(Page page)
        {
            if (ModelState.IsValid)
            {
                var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == page.page_id).FirstOrDefault();
                page.page_id = Prepage.page_id;
                MvcApplication._MainCustomer.ContentPages.Remove(Prepage);
                MvcApplication._MainCustomer.ContentPages.Add(page);


                return RedirectToAction("EditWebsite", "AdminCustomer");
            }


            return View(page);
        }

        //
        // GET: /Page/Delete/5

        [AcceptVerbs(HttpVerbs.Delete)]
        public string Delete(int id)
        {
            var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == id).FirstOrDefault();

            MvcApplication._MainCustomer.ContentPages.Remove(Prepage);


            return "yes";
        }

        public string DeleteContactUs(int id)
        {
            var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == id).FirstOrDefault();
            MvcApplication._MainCustomer.Website.IsContactusPage = false;
            MvcApplication.pageId = 0;
            MvcApplication._MainCustomer.ContentPages.Remove(Prepage);
            return "yes";

        }

        public string AddContactUS(int id)
        {
            var Prepage = db.Pages.Where(p => p.page_id == id).FirstOrDefault();
            MvcApplication._MainCustomer.Website.IsContactusPage = true;
            Prepage.page_id = MvcApplication._MainCustomer.ContentPages.Count() == 0 ? 1 : MvcApplication._MainCustomer.ContentPages.Select(cp => cp.page_id).Max() + 1;
            MvcApplication._MainCustomer.ContentPages.Add(Prepage);
            MvcApplication.pageId = Prepage.page_id;
            return "done";
        }

    }
}